package src.E_Thread;
/*
题目：
龟兔赛跑程序，设赛跑长度为30米
兔子的速度 10米每秒， 兔子每跑完10米休眠10秒
乌龟的速度 1米每秒， 乌龟每跑完10米休眠1秒
要求：等兔子和乌龟的线程结束时，公布结果
提示：使用System.currentTimeMillis()
*/
public class E04_TheHareAndTheTortoise{
    public static void main(String[] args) {
        Hare hare = new Hare();
        Tortoise tortoise = new Tortoise();
        Thread hareThread = new Thread(hare,"兔子");
        Thread tortoiseThread = new Thread(tortoise,"乌龟");
        hareThread.start();
        tortoiseThread.start();
        // 让线程执行完再执行下面的代码
        try {
            hareThread.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        try {
            tortoiseThread.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        // 拿总用时作比较
        long tortoiseTime = tortoise.getTortoiseTime();
        long hareTime = hare.getHareTime();
        System.out.println("乌龟用时 "+tortoiseTime);
        System.out.println("兔子用时 "+hareTime);
        if (hareTime > tortoiseTime){
            System.out.println("乌龟胜！");
        } else if (hareTime < tortoiseTime){
            System.out.println("兔子胜！");
        }else {
            System.out.println("平局");
        }
    }
}

// 兔子类
class Hare implements Runnable{
    private long hareTime;

    public long getHareTime() {
        return hareTime;
    }

    public void setHareTime(long time) {
        this.hareTime = time;
    }

    @Override
    public void run() {
        // 开始时间
        long hareStart = System.currentTimeMillis();
        for (int i = 0; i < 30; i++) {
            // 模拟兔子速度，0.1s/m
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            // 打印记录里程
            System.out.println(Thread.currentThread().getName()+" 跑了"+i+" 米");
            // 模拟兔子休息
            if (i%10==0 && i!=0){
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        // 结束时间
        long hareEnd = System.currentTimeMillis();
        // 总时长
        this.hareTime = hareEnd - hareStart;
    }
}

// 乌龟类
class Tortoise implements Runnable{
    private long tortoiseTime;

    public long getTortoiseTime() {
        return tortoiseTime;
    }

    public void setTortoiseTime(long tortoiseTime) {
        this.tortoiseTime = tortoiseTime;
    }

    @Override
    public void run() {
        // 开始时间
        long tortoiseStart = System.currentTimeMillis();
        for (int i = 0; i < 30; i++) {
            // 模拟乌龟速度，1s/m
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            // 打印记录里程
            System.out.println(Thread.currentThread().getName()+" 跑了"+i+" 米");
            // 模拟乌龟休息
            if (i%10==0 && i!=0){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        // 结束时间
        long tortoiseEnd = System.currentTimeMillis();
        // 总时长
        this.tortoiseTime = tortoiseEnd - tortoiseStart;
    }
}